package xin.sunzy.spring;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import xin.sunzy.spring.intercept.UserLoginIntercept;
import xin.sunzy.spring.util.ResponseUtils;

import java.util.concurrent.*;

/**
 * @description: 应用配置
 * @email: <a href="sunzhenyu@terminus.io">孙振宇</a>
 * @author: ginkgo
 * @date: 2018/4/2
 */
@Configuration
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {

    /**
     * 国际化资源管理
     *
     * @return
     */
    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.addBasenames("classpath:messages");
        messageSource.setCacheSeconds(3600);
        messageSource.setUseCodeAsDefaultMessage(true);
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    /**
     * 请求响应工具类
     *
     * @param messageSource
     * @return
     */
    @Bean
    public ResponseUtils responseUtils(@Autowired MessageSource messageSource) {
        return new ResponseUtils(messageSource);
    }

    /**
     * 事件中心
     *
     * @return
     */
    @Bean
    public EventBus eventBus() {
        String identifier = "zys-event-bus";
        //线程工厂
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("zys-event-bus-pool-%d").build();
        //核心线程数
        int corePoolSize = 5;
        //线程池最大容量
        int maximumPoolSize = 200;
        //空闲线程存活时间
        long keepAliveTime = 0L;
        //队列
        LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(1024);
        //拒绝策略
        RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.AbortPolicy();
        //线程池
        ExecutorService executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
                keepAliveTime, TimeUnit.MILLISECONDS, workQueue, threadFactory, rejectedExecutionHandler);

        return new AsyncEventBus(identifier, executor);
    }

    @Autowired
    private UserLoginIntercept userLoginIntercept;

    @Bean
    public UserLoginIntercept userLoginIntercept() {
        return new UserLoginIntercept();
    }

    /**
     * 添加静态资源
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/local-file/**").addResourceLocations("file:/var/");
        super.addResourceHandlers(registry);
    }

    /**
     * 配置用户登录的拦截器
     *
     * @return
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userLoginIntercept).addPathPatterns("/**").excludePathPatterns("/*.icon");
        super.addInterceptors(registry);
    }
}
